"""
Utility functions to return a formatted name and description for a given view.

返回给定视图的格式化名称和描述的实用函数
"""

from my_typing import *


class lazy_format:  # noqa
    """
    Delay formatting until it"s actually needed.
    延迟格式化直到实际需要时

    Useful when the format string or one of the arguments is lazy.
    当格式字符串或其中一个参数是延迟加载时非常有用

    Not using Django"s lazy because it is too slow.
    不使用 Django 的延迟加载，因为它的速度太慢了
    """

    __slots__ = ("format_string", "args", "kwargs", "result")

    def __init__(
        self,
        format_string: str,
        *args,
        **kwargs,
    ) -> None:
        self.result: StrOrNone = None
        self.format_string: str = format_string
        self.args = args
        self.kwargs = kwargs

    def __str__(self) -> str:
        if self.result is None:
            self.result = self.format_string.format(*self.args, **self.kwargs)
            self.format_string, self.args, self.kwargs = None, None, None

        return self.result

    def __mod__(self, value) -> str:
        return str(self) % value
